{extend name="lib/common_list" /}
{block name="title"}{/block}
{block name="css"}
{load href="/static/sys/css/common.css" /}
{load href="/static/plugins/select2/css/select2.css" /}
{load href="/static/plugins/bootstrap-datetimepicker/css/bootstrap-datetimepicker.css" /}

{load href="/static/sys/css/skin_0.css" /}
<style>
    .upfile i{padding-right:3px !important;}
    .file{ position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;margin-bottom: 0px;margin-right: 0px; }
</style>
{/block}
{block name="javascript"}
{load  href="/static/plugins/select2/js/select2.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/bootstrap-datetimepicker.js" /}
{load  href="/static/plugins/bootstrap-datetimepicker/js/locales/bootstrap-datetimepicker.zh-CN.js" /}

<script>
    $(document).ready(function () {
        $("#ok").bind("click", Save);
        $("#cancel").bind("click", function () {
            parent.layer.close(parent.layer_add_index);
        });

        $('#applet_id').select2();

        /**
         * 保存内容
         */
        function Save() {
            var load_index = layer.load(0, { shade: false });

            $.ajax({
                type: 'POST',
                url: "{:url('button/add')}",
                data: $("#fmNew").serialize(),
                dataType: 'json',
                success: function (data) {
                    if (data.code == 1) {
                        layer.close(load_index);
                        layer.alert(data.msg, function () {
                            parent.refreshtb();
                            //window.close();
                            parent.layer.close(parent.layer_add_index);
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                }
            })
        }

        // 时间插件
        $('.form_datetime').datetimepicker({
            weekStart: 0, //一周从哪一天开始
            todayBtn:  1, //
            autoclose: 1,
            todayHighlight: 1,
            startView: 2,
            forceParse: 0,
            //showMeridian: 1,
            language:'zh-CN',
            format: 'yyyy-mm-dd',
            minView:'month',
        });

        $('#cate_name').select2();
    });

    /**
     * 新增图片
     */
    var layer_image_add;
    function button() {
        layer_image_add = layer.open({
            type: 2,
            title: "新增图片",
            area: ['70%', '90%'],
            content: "{:url('upload/image')}?tag=button"
        })
    }

    // 上传成功后的回调，该函数名不能写错
    function getImageUrl(res) {
       if (res.tag == 'button') {
            $('#image_url').val(res.url);
            $('#previewImg_button').attr('src', res.url);
        }
    }

    function typeValue() {
        var type = $("#type_name").children('option:selected').attr('type');
        $('#type').val(type);
        if (type == 'TYPE_APPLET') {
            $('#type_applet').show();
            $('#type_link_and_local').hide();
            $('#link').val('');
            //$('#image_url').val('');
            //$('#previewImg_button').attr('src', '');
        } else if (type == 'TYPE_LINK' || type == 'TYPE_LOCAL') {
            $('#type_link_and_local').show();
            $('#type_applet').hide();
            $('#applet_app_id').val('');
            $('#applet_path').val('');
            //$('#applet_image').val('');
        }
    }

    function appletValue() {
        var real_app_id = $('#applet_name').children('option:selected').attr('real_app_id');
        $('#applet_app_id').val(real_app_id);
    }

</script>
{/block}

{block name="content"}
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <form method="post" id="fmNew" class="form-horizontal" enctype="multipart/form-data">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮名称：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="name" placeholder="请输入名称">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮键名：</label>
                            <div class="col-sm-8">
                                <input type="text" class="form-control" name="key" placeholder="请输入唯一的键名,命名之后不可修改">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮分组：</label>
                            <div class="col-sm-8">
                                <select class="form-control" id="group_name" name="group_name">
                                    <option value="">请选择按钮分组...</option>
                                    {volist name="group_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮类型：</label>
                            <div class="col-sm-8">
                                <select class="form-control" id="type_name" onchange="typeValue()">
                                    <option value="">请选择按钮类型...</option>
                                    {volist name="type_list" id="v"}
                                    <option value="{$v}" type="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div id="type_applet" hidden>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">小程序：</label>
                                <div class="col-sm-8">
                                    <select class="form-control" id="applet_name" name="applet_name" onchange="appletValue()">
                                        <option value="">请选择小程序...</option>
                                        {volist name="applet_list" id="v"}
                                        <option value="{$v.applet_name}" real_app_id="{$v.real_app_id}">{$v.applet_name}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">小程序APP ID：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="applet_app_id" class="form-control"  name="applet_app_id" readonly>
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>

                            <div class="form-group">
                                <label class="col-sm-2 control-label">小程序路径：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="applet_path" class="form-control"  name="applet_path" placeholder="请输入小程序路径">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>

                            <!--<div class="form-group">
                                <label class="col-sm-2 control-label">小程序跳转封面：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="applet_image" class="form-control"  name="applet_image_url" placeholder="请输入小程序跳转封面图">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>-->
                        </div>

                        <div id="type_link_and_local" hidden>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">跳转链接：</label>
                                <div class="col-sm-8">
                                    <input type="text" id="link" class="form-control" name="link" placeholder="请输入跳转的网页链接">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">按钮图：</label>

                            <div class="col-sm-1">
                                <span id="button_img" class="btn btn-primary btn-sm" style="margin-bottom:0;padding:6px 8px;" onclick="button()">
                                    <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 按钮图<input type="hidden" name="image_url" id="image_url" />
                                </span>
                            </div>
                            <div class="col-sm-1">
                                <img id="previewImg_button" style="height:34px;float: right;">
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">状态：</label>
                            <div class="col-sm-8">
                                <select class="form-control" name="status" id="status">
                                    {volist name="status_list" id="v"}
                                    <option value="{$key}">{$v}</option>
                                    {/volist}
                                </select>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">描叙：</label>
                            <div class="col-sm-8">
                                <textarea cols="100%" rows="3" name="desc"></textarea>
                            </div>
                        </div>
                        <div class="hr-line-dashed"></div>

                        <div id = 'hidden_input'>
                            <input type="hidden" id="type" name="type"/>
                        </div>

                        <div class="form-group">
                            <div class="col-sm-12 col-sm-offset-5">
                                <button class="btn btn-primary" id="ok" type="button">提交</button>
                                <button class="btn btn-white" id="cancel" type="button">取消</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{/block}